package org.roger.search;

import java.io.IOException;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queries.CustomScoreProvider;
import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.queries.function.FunctionQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.roger.valuesource.SelfValueSource;

/**
 * 用户自定义评分查询
 * @author luoweiyi
 *
 */
public class CustomScoreSearch extends LuceneSearchBase{
	
	

	@Override
	public Query getSearchQuery() {
		
		SingleSearch search = new SingleSearch("contents", "your");
	
		CustomScoreQuery query = new CustomScoreQuery(search.getSearchQuery(),new FunctionQuery(new SelfValueSource(search.getSearchQuery(),1.0f))){
			public CustomScoreProvider getCustomScoreProvider(AtomicReaderContext r){
				return new CustomScoreProvider(r){
					public float customScore(int doc, float subQueryScore, float valSrcScore){
						return 6.0f;
					}
				};
			}
		};
		
		
		return query;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static void main(String[] args) {
		CustomScoreSearch cs = new CustomScoreSearch();
		try {
			cs.search("D:/index");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
